package cn.appsys.controller.developer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;

@Controller
@RequestMapping("/dev")
public class DevLoginController {

	private Logger logger = Logger.getLogger(DevLoginController.class);

	@Resource
	private DevUserService devUserService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "devlogin";
	}

	@RequestMapping(value = "/dologin", method = RequestMethod.POST)
	public String doLogin(@RequestParam String devCode, @RequestParam String devPassword, HttpServletRequest request) {
		logger.debug("devCode ======>" + devCode);
		logger.debug("devPassword ======>" + devPassword);
		DevUser devUser = devUserService.login(devCode);
		if (devUser != null) {
			if (devPassword.equals(devUser.getDevPassword())) {
				request.getSession().setAttribute(Constants.DEV_USER_SESSION, devUser);
				return "redirect:main";
			}
			request.setAttribute("error", "密码不正确！");
			return "devlogin";
		}
		request.setAttribute("error", "没有该账户！");
		return "devlogin";
	}

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		request.removeAttribute("error");
		request.getSession().removeAttribute(Constants.DEV_USER_SESSION);
		return "devlogin";
	}

	@RequestMapping("/main")
	public String main() {
		return "developer/main";
	}
}
